package cn.chenyumeng.blog.controller.blog;

import cn.chenyumeng.blog.dto.BlogDTO;
import cn.chenyumeng.blog.dto.CommonResponse;
import cn.chenyumeng.blog.dto.ResultCode;
import cn.chenyumeng.blog.service.blog.HomePageService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: blog->HomePageController
 * @description:
 * @author: cg
 * @create: 2020-08-10 18:29
 **/
@Validated
@RestController
@ResponseBody
@RequestMapping("/blog/home")
public class HomePageController {

    @Autowired
    private HomePageService homePageService;

    @GetMapping("/queryAllByLimit")
    public CommonResponse queryAllByLimit(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                          @RequestParam(name = "size", defaultValue = "8") Integer size) {
        PageInfo<BlogDTO> blogPageInfo = homePageService.queryAllByLimit(page, size);
        if (blogPageInfo == null) {
            return CommonResponse.build(ResultCode.PARAM_IS_INVALID);
        }
        return CommonResponse.ok(blogPageInfo);
    }

    @GetMapping("/queryBlogLatestRecommend")
    public CommonResponse queryBlogLatestRecommend(@RequestParam(name = "start", defaultValue = "0") Integer start,
                                                   @RequestParam(name = "end", defaultValue = "6") Integer end) {
        List<BlogDTO> list = homePageService.queryBlogLatestRecommend(start, end);
        if (list == null) {
            return CommonResponse.build(ResultCode.PARAM_IS_INVALID);
        }
        return CommonResponse.ok(list);
    }

    @GetMapping("/search")
    public CommonResponse search(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                 @RequestParam(name = "size", defaultValue = "6") Integer size,
                                 @RequestParam(name = "content") String content) {
        PageInfo<BlogDTO> search = homePageService.search(content, page, size);
        if (search == null) {
            return CommonResponse.build(ResultCode.PARAM_IS_INVALID);
        }
        return CommonResponse.ok(search);
    }

}
